package contacts;

import files.FileHelper;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;

import static junit.framework.Assert.assertEquals;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

/**
 * Created by IntelliJ IDEA.
 * User: Jaan
 * Date: 19.11.11
 * Time: 22:42
 */
public class ContactHelperTest {
  private ContactHelper contactHelper;

  @Before
  public void setup() {
    contactHelper = new ContactHelper();
    contactHelper.fileHelper = mock(FileHelper.class);
  }

  @Test
  public void parseContacts() throws Exception {
    String contacts = "[\n[\"Tanel Tammet\",\"22.33.44.55:6666\"],\n[\"Peeter Laud\",\"22.33.44.11:6666\"]\n]";
    ArrayList<Contact> myContacts = contactHelper.parseContacts(contacts);

    assertEquals(2, contactHelper.parseContacts(contacts).size());

    assertEquals("Tanel Tammet", myContacts.get(0).getName());
    assertEquals("22.33.44.55:6666", myContacts.get(0).getIPAndPort());
    assertEquals("Peeter Laud", myContacts.get(1).getName());
    assertEquals("22.33.44.11:6666", myContacts.get(1).getIPAndPort());
  }

  @Test
  public void saveContacts() {
    ArrayList<Contact> contacts = new ArrayList<Contact>();
    contacts.add(new Contact("Mati Naganen", "11.22.33.44:6666"));
    contacts.add(new Contact("Kalle Blomkvist", "192.168.0.162:6666"));
    contacts.add(new Contact("Rait Raud", "44.32.66.199:6666"));


    String contents = "[\n[\"Mati Naganen\",\"11.22.33.44:6666\"],\n[\"Kalle Blomkvist\",\"192.168.0.162:6666\"],\n[\"Rait Raud\",\"44.32.66.199:6666\"]\n]";

    contactHelper.saveContacts(contacts);
    verify(contactHelper.fileHelper).writeToFile(anyString(), anyString());
  }

}
